package customer.zspt.cn.customer.components.contact;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import customer.zspt.cn.customer.Adapter.ContactsAdapter;
import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.Widget.ContactLetterRightView;
import customer.zspt.cn.customer.components.group.group;
import customer.zspt.cn.customer.components.servex.servex;
import customer.zspt.cn.customer.components.tag.tag;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.bean.PersonalIDao;
import customer.zspt.cn.customer.utils.ContentComparator;
import customer.zspt.cn.customer.utils.IntentUtils;
import customer.zspt.cn.customer.utils.SPUtils;

/**
 * Created by Administrator on 2018/6/7 0007.通讯录
 */

public class contact extends Fragment {
    //通讯录的列表
    @ViewInject(R.id.Address_ls)
    private ListView Address_ls;
    //右侧字母索引
    @ViewInject(R.id.RightView)
    private ContactLetterRightView Adress_ll;
    //字母的列表
    @ViewInject(R.id.indexical_item)
    private LinearLayout indexical_item;
    private List<PersonalIDao.userList> name = new ArrayList<>();
    //字母索引的独立控件的高
    private int height;
    //是否要进行通讯录数据的准备
    private boolean IsSetDate = true;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.contact, null);
        ViewUtils.inject(this, view);
        setData();
        setLister();
        return view;
    }
    private void setLister() {
        //通讯录列表的适配器
        final ContactsAdapter adapter = new ContactsAdapter(getActivity(), name);
        final TextView content = indexical_item.findViewById(R.id.indexical_title);
        Adress_ll.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                height = Adress_ll.getChildAt(0).getHeight();
                Adress_ll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                Adress_ll.setHeight(height);
            }
        });
        Adress_ll.setCharacterListener(new ContactLetterRightView.CharacterClickListener() {
            @Override
            public void clickCharacter(char character) {
                content.setText(character + "");
                indexical_item.setVisibility(View.VISIBLE);
                int positionForSection = adapter.getPositionForSection(character);
                if (positionForSection != -1) {
                    Address_ls.setSelectionFromTop(positionForSection + 3, 0);
                }
            }
            @Override
            public void clickArrow() {
                Address_ls.setSelectionFromTop(0, 0);
            }
            @Override
            public void GoneView() {
                indexical_item.setVisibility(View.GONE);
            }
        });
        Address_ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if (position > 2) {
                    Intent intent = new Intent(getActivity(), contact_view.class);
                    intent.putExtra("kefuId", name.get(position - 3).kefuId);
                    startActivity(intent);
                } else if (position == 2) {
                    IntentUtils.startIntent(getActivity(), servex.class);
                } else if (position == 0) {
                    IntentUtils.startIntent(getActivity(), group.class);
                } else if (position == 1) {
                    IntentUtils.startIntent(getActivity(), tag.class);
                }
            }
        });
        Address_ls.setAdapter(adapter);
    }
    /*** 准备通讯录的数据*/
    public void setData() {
        //如果数据已经准备好，则跳过
        if(!IsSetDate)return;
        String ContaceData = SPUtils.getString(getContext(), constant.KEFU, "");
        if(TextUtils.isEmpty(ContaceData)){IsSetDate = true;}
        try{
            SortContactData(ContaceData);
            IsSetDate = false;
        }catch (Exception e){
            IsSetDate = true;
            e.printStackTrace();
        }
    }
    /**排序通讯录数据*/
    private void SortContactData(String contaceData) {
        Gson gson = new Gson();
        PersonalIDao personalI = gson.fromJson(contaceData, PersonalIDao.class);
        name = personalI.data;
        Collections.sort(personalI.data, new ContentComparator());
        Collections.reverse(personalI.data);
    }
}
